package com.yangfei.SportAssistant.services;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKLocationManager;
import com.yangfei.SportAssistant.util.Constants;

import android.location.Location;

public class CBaiduLocationListener extends MapListsenerSuper {

	LocationListener mLocationListener = null; 
	BMapManager mBMapMan = null;
	private MKLocationManager locationManager = null;
	MapServiceInterface mService = null;

	public void Init(MapServiceInterface aService) {
		mService = aService;
		listsenerSelf = this;

		mBMapMan = new BMapManager(mService.GetContext());
		mBMapMan.init(Constants.mStrKey, null);
		locationManager = mBMapMan.getLocationManager();
		locationManager.enableProvider(MKLocationManager.MK_GPS_PROVIDER);
		mapProviderName = Constants.BaiduMap;
		mLocationListener = new LocationListener() {
			@Override
			public void onLocationChanged(Location location) {
				mService.DoLocationChanged(location, listsenerSelf);
			}
		};
	}

	public void StartListen() {
		if (mBMapMan != null)
			mBMapMan.start();
		locationManager.requestLocationUpdates(mLocationListener);
		ResetLocationFilter();
	}

	public void StartListen(long minInterval, int minDistance) {
		// todo: do not know how to set the parameter of maxinterval,so just set
		// it to mininterval*5
		if (mBMapMan != null)
			mBMapMan.start();
		locationManager.setNotifyInternal((int) ( (minInterval/1000) ), (int) (minInterval/1000));
		locationManager.requestLocationUpdates(mLocationListener);
	}

	public boolean IsManagerReady() {
		if (locationManager == null)
			return false;
		else
			return true;
	}

	public void StopListen() {
		if (mBMapMan != null)
			mBMapMan.stop();
		locationManager.removeUpdates(mLocationListener);
	}

	public void Destroy() {
		// Final cleanup before activity is destroyed.
		// May not be called at all in some situations.
		if (mBMapMan != null) {			
			mBMapMan.destroy();
			mBMapMan = null;
		}

	}

	public Object GetMapMag() {
		return (Object)mBMapMan;
	}

}
